package cn.chrilwe.pigpig.im.config;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.PropertySource;
import org.springframework.util.StringUtils;

import cn.chrilwe.pigpig.im.service.auth.Oauth2Service;
import cn.chrilwe.pigpig.im.service.auth.mark.Oauth2Mark;

/**
 * 
 * @author chrilwe
 * 2020-7-28
 */
@Configuration
@EnableAspectJAutoProxy
@EnableAutoConfiguration
@EnableEurekaClient
@PropertySource(value = "application.properties")
public class SpringConfig {
	
	@Value("${spring.application.name}")
	private String serverName;
	
	@Value("${server.port}")
	private int port;
	
	@Value("${server.host}")
	private String host;
	
	@Value("${server.shards}")
	private String shards;

	@Bean
	public ServerConfig config() {
		ServerConfig serverConfig = new ServerConfig();
		serverConfig.setPort(port);
		serverConfig.setWsUrl("/ws");
		serverConfig.setHost(host);
		serverConfig.setServerName(serverName);
		
		List<String> l = getShards();
		if(l != null && l.size() > 0) {
			serverConfig.setShards(l);
		}
		return serverConfig;
	}
	
	protected List<String> getShards() {
		if(!StringUtils.isEmpty(shards)) {
			List<String> l = new ArrayList<String>();
			String[] split = shards.split(",");
			for (String serverName : split) {
				l.add(serverName);
			}
			return l;
		}
		return null;
	}
	
	//校验方式
	@Bean
	public Oauth2Mark authMark() {
		
		return new Oauth2Mark();
	}
	
}
